﻿using System;
using System.Collections.Generic;
using personalplaner.business.common;
using personalplaner.business.enums;
using personalplaner.business.stamm;
using personalplaner.dataprovider;
using personalplaner.common;
using System.Linq;

namespace personalplaner.servicemodel.dataaccess
{
    public interface IPersonDataAccess
    {
		#region Get Methods

        IEnumerable<Person> GetPersonen(int prsId = 0, bool showAll = false);

		Person GetPerson(int prsid);

		IEnumerable<Holiday> GetHolidays(int prsid, int year);

		IEnumerable<Holiday> GetHolidays(int prsid, int year, int month);

		#endregion

		#region Set Methods
		
		int SavePerson(Person person, DateTime anstellungdatumvon, DateTime anstellungdatumbis, int anstellungid);

		/// <summary>
		/// adds a new holiday and returns the new ferienid
		/// </summary>
		/// <param name="holiday"></param>
		/// <returns></returns>
		int SetHoliday(Holiday holiday);

		#endregion

		#region Update Methods

		int UpdatePerson(Person person/*, DateTime anstellungdatumvon, DateTime anstellungdatumbis, int anstellungid*/);

		#endregion

		#region Delete Methods

		bool DeletePerson(Person person);

		bool DeleteHoliday(int ferienid);

		#endregion
	}

    public class PersonDataAccess : BaseDataAccess, IPersonDataAccess
    {
        public PersonDataAccess(ISettingsProvider settings)
            : base(settings)
        {
        }

		#region Get Methods

        public IEnumerable<Person> GetPersonen(int prsId = 0, bool showAll = false)
        {
			using (new WaitCursor())
			{
				try
				{
					enPersonTyp typ = enPersonTyp.Stammperson;
					if (showAll)
						typ = enPersonTyp.Alle;

                    return DataServices.PersonalService.GetPersonen(Settings.DatabaseSettings, Settings.Mandant, typ: typ, prsid: prsId);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return null;
			}
        }

		public Person GetPerson(int prsid)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PersonalService.GetPersonen(Settings.DatabaseSettings, Settings.Mandant, typ: enPersonTyp.Alle,prsid:prsid).First();
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return null;
			}
		}

		public IEnumerable<Holiday> GetHolidays(int prsid, int year)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PersonalService.GetHolidays(Settings.DatabaseSettings, prsid, year);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return null;
			}
		}

		public IEnumerable<Holiday> GetHolidays(int prsid, int year, int month)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PersonalService.GetHolidays(Settings.DatabaseSettings, prsid, year, month);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return null;
			}
		}

    	#endregion

		#region Set Methods

		public int SavePerson(Person person, DateTime anstellungdatumvon, DateTime anstellungdatumbis, int anstellungid)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PersonalService.SavePerson(Settings.DatabaseSettings, person, Settings.Mandant, anstellungdatumvon, anstellungdatumbis, anstellungid);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return 0;
			}
		}

		/// <summary>
		/// adds a new holiday and returns the new ferienid
		/// </summary>
		/// <param name="holiday"></param>
		/// <returns></returns>
		public int SetHoliday(Holiday holiday)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PersonalService.SetHoliday(Settings.DatabaseSettings, holiday);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return 0;
			}
		}

		#endregion

		#region Update Methods

		public int UpdatePerson(Person person/*, DateTime anstellungdatumvon, DateTime anstellungdatumbis, int anstellungid*/)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PersonalService.UpdatePerson(Settings.DatabaseSettings, person/*, anstellungdatumvon, anstellungdatumbis, anstellungid*/);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return 0;
			}
		}

		#endregion

		#region Delete Methods

		public bool DeletePerson(Person person)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PersonalService.DeletePerson(Settings.DatabaseSettings, person);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return false;
			}
		}

		public bool DeleteHoliday(int ferienid)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PersonalService.DeleteHoliday(Settings.DatabaseSettings, ferienid);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return false;
			}
		}

    	#endregion
	}
}
